package where2go.sctu.com.zhiyou.fragment;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.activity.LineDetailsActivity;
import where2go.sctu.com.zhiyou.adpater.RecyclerViewOneAdapter;
import where2go.sctu.com.zhiyou.adpater.RecyclerViewThreeAdapter;
import where2go.sctu.com.zhiyou.adpater.RecyclerViewTwoAdapter;
import where2go.sctu.com.zhiyou.db.ScrollLinearLayoutManager;
import where2go.sctu.com.zhiyou.entity.PersonRecomClass;
import where2go.sctu.com.zhiyou.utils.C;
import where2go.sctu.com.zhiyou.view.ImageBarnnerFramLayout;

/**
 * ProjectName: zhiyou
 * Created by 张某人
 * CreatedTime:on 2018/4/25   23:02
 * Description: 首页fragment
 */

public class HomeFragment extends Fragment implements View.OnClickListener {

    private ImageBarnnerFramLayout mGroup;

    private RecyclerView recyclerView;
    private RecyclerViewOneAdapter oneAdapter;
    private RecyclerViewTwoAdapter twoAdapter;
    private RecyclerViewThreeAdapter threeAdapter;
    private List<PersonRecomClass.DataBean> courseList = new ArrayList<PersonRecomClass.DataBean>();

    private Button btnPersonal, btnHot, btnRecent, btnSpecial;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    private String urlPersonal = "http://47.104.80.155/youyiyou/class/classlatest?page=1";
    private String urlHot = "http://47.104.80.155/youyiyou/class/classmoststudy?page=1";
    private String urlNewest = "http://47.104.80.155/youyiyou/class/classlatest?page=1";
    private String urlSpecial = "http://47.104.80.155/youyiyou/class/classlatest?page=1";


    //存放轮播图片
    private int[] ids = new int[]{
            R.drawable.img1, R.drawable.img2, R.drawable.img3,R.drawable.img4
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);


        findView(view);
        return view;
    }



    //初始化控件，View
    private void initView(View view) {
        mGroup = (ImageBarnnerFramLayout) view.findViewById(R.id.image_group);
        recyclerView = (RecyclerView) view.findViewById(R.id.mRecycleView);
        btnPersonal = (Button) view.findViewById(R.id.btn_personal);
        btnPersonal.setOnClickListener(this);
        btnHot = (Button) view.findViewById(R.id.btn_hot);
        btnHot.setOnClickListener(this);
        btnRecent = (Button) view.findViewById(R.id.btn_recent);
        btnRecent.setOnClickListener(this);
        btnSpecial = (Button) view.findViewById(R.id.btn_special);
        btnSpecial.setOnClickListener(this);
    }

    private void findView(View view) {

        initView(view);
        //轮播图
        //我们需要求出我们当前手机的宽度
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        C.WIDTH = dm.widthPixels;

        List<Bitmap> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), ids[i]);
            list.add(bitmap);
        }
        mGroup.addBitmaps(list);

        //content
        recyclerView.setLayoutManager(new ScrollLinearLayoutManager(getActivity(),
                ScrollLinearLayoutManager.VERTICAL, false));
        courseList = new ArrayList<PersonRecomClass.DataBean>();
        loaderDataTwo(urlPersonal);
    }

    private void loaderDataOne(String url) {
        //        开始使用volley获取数据
//        第二部：选择请求对象
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {  //onPostExecute
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                final PersonRecomClass personRecomClass = gson.fromJson(s, PersonRecomClass.class);
                courseList = personRecomClass.getData();
                oneAdapter = new RecyclerViewOneAdapter(courseList, getContext());
                oneAdapter.setItemClickListener(new RecyclerViewOneAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //跳转到详情页
                        Intent intent=new Intent();
                        intent.setClass(getActivity(), LineDetailsActivity.class);

                        //用Bundle携带数据
                        Bundle bundle=new Bundle();
                        int classId=courseList.get(position).getClassId();
                        intent.putExtra("classId",classId);
                        bundle.putString("classId", String.valueOf(classId));
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);

                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
                recyclerView.setAdapter(oneAdapter);
                oneAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG", volleyError.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
//        第三部：把请求对象加入到请求的队列当中
        });
        MyApplication.getHttpQueues().add(request);
    }

    private void loaderDataTwo(String url) {
        //        开始使用volley获取数据
//        第二部：选择请求对象
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {  //onPostExecute
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                PersonRecomClass personRecomClass = gson.fromJson(s, PersonRecomClass.class);
                courseList = personRecomClass.getData();
                twoAdapter = new RecyclerViewTwoAdapter(courseList, getContext());
                twoAdapter.setItemClickListener(new RecyclerViewTwoAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //跳转到详情页
                        Intent intent=new Intent();
                        intent.setClass(getActivity(), LineDetailsActivity.class);

                        //用Bundle携带数据
                        Bundle bundle=new Bundle();
                        int classId=courseList.get(position).getClassId();
                        bundle.putString("classId", String.valueOf(classId));
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
                recyclerView.setAdapter(twoAdapter);
                twoAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG", volleyError.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
//        第三部：把请求对象加入到请求的队列当中
        });
        MyApplication.getHttpQueues().add(request);
    }

    private void loaderDataThree(String url) {
        //        开始使用volley获取数据
//        第二部：选择请求对象
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {  //onPostExecute
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                PersonRecomClass personRecomClass = gson.fromJson(s, PersonRecomClass.class);
                courseList = personRecomClass.getData();
                threeAdapter = new RecyclerViewThreeAdapter(courseList, getContext());
               threeAdapter.setItemClickListener(new RecyclerViewThreeAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //跳转到详情页
                        Intent intent=new Intent();
                        intent.setClass(getActivity(), LineDetailsActivity.class);
                        //用Bundle携带数据
                        Bundle bundle=new Bundle();
                        int classId=courseList.get(position).getClassId();
                        bundle.putString("classId", String.valueOf(classId));
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {
                        Toast.makeText(getContext(),"LongPosition"+position,
                                Toast.LENGTH_SHORT).show();
                    }
                });
                recyclerView.setAdapter(threeAdapter);
                threeAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG", volleyError.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
    //        第三部：把请求对象加入到请求的队列当中
        });
        MyApplication.getHttpQueues().add(request);
    }

    @Override
    public void onClick(View v) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        switch (v.getId()) {
            case R.id.btn_personal:
                recyclerView.setLayoutManager(new ScrollLinearLayoutManager(getContext(),
                        ScrollLinearLayoutManager.VERTICAL, false));
                courseList = new ArrayList<PersonRecomClass.DataBean>();
                loaderDataTwo(urlPersonal);
                break;
            case R.id.btn_hot:
                recyclerView.setLayoutManager(gridLayoutManager);
                courseList = new ArrayList<PersonRecomClass.DataBean>();
                loaderDataOne(urlHot);
                break;
            case R.id.btn_recent:
                recyclerView.setLayoutManager(new ScrollLinearLayoutManager(getContext(),
                        ScrollLinearLayoutManager.VERTICAL, false));
                courseList = new ArrayList<PersonRecomClass.DataBean>();
                loaderDataThree(urlNewest);
                break;
            case R.id.btn_special:
                recyclerView.setLayoutManager(gridLayoutManager);
                courseList = new ArrayList<PersonRecomClass.DataBean>();
                loaderDataOne(urlSpecial);
                break;
            default:
                break;
        }
    }
}
